#include <vector>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

#include "include/CharacterCtrl.h"
#include "include/Character.h"
#include "include/Utils.h"
#include "include/EventParser.h"

using namespace boost::property_tree;

void CharacterCtrl::CreateRole(GlobalGameObj *p_game_obj)
{
	Character::BasicInfo basic_info;

	try
	{
		basic_info.name = p_game_obj->in_tree.get<string>("name");
		basic_info.gender = p_game_obj->in_tree.get<int>("gender");
		basic_info.job = p_game_obj->in_tree.get<int>("job");
	}
	catch(exception &ex)
	{
		p_game_obj->error_code = Common::JsonTreeError;
		p_game_obj->error_str = "in CreateRole";
		return;
	}

	basic_info.character_id = Utils::GetUniqueId();
	basic_info.player_id = (int)p_game_obj->p_package_head->player_id;
	basic_info.level = 1;
	// todo fill other fields

	Character::DetailInfo detail_info;
	detail_info.character_id = basic_info.character_id;
	detail_info.name = basic_info.name;
	detail_info.job = basic_info.job;
	detail_info.level = basic_info.level;
	// todo fill other fields

	p_game_obj->model_proxy.BeginTransaction();

	Character::CreateBasicInfo(p_game_obj, &basic_info);
	if(p_game_obj->error_code)
	{
		p_game_obj->model_proxy.BreakTransaction();
		return;
	}

	Character::CreateDetailInfo(p_game_obj, &detail_info);
	if(p_game_obj->error_code)
	{
		p_game_obj->model_proxy.BreakTransaction();
		return;
	}

	p_game_obj->model_proxy.EndTransaction();
}

void CharacterCtrl::GetBaseInfo(GlobalGameObj *p_game_obj)
{
#ifdef _DEBUG_LEVEL_1_
	cout << __FUNCTION__ << " in" << endl;
#endif

	int character_id;
	try
	{
	    // because player can query other player's info, character id is needed
		character_id = p_game_obj->in_tree.get<int>("characterId");

	}
	catch(exception &ex)
	{
		p_game_obj->error_code = Common::JsonTreeError;
		p_game_obj->error_str = "in GetBaseInfo";
		return;
	}

	ptree info_tree;
	Character::GetBasicInfoTree(p_game_obj, character_id, info_tree);
	if(Common::NoError != p_game_obj->error_code)
	{
	    return;
	}

	info_tree.put("evt", "Character_BaseInfo");
	info_tree.put("code", 0);

	p_game_obj->out_tree.push_back(make_pair("", info_tree));

	p_game_obj->out_format_tree.put("dataType", Common::NormalData);

	cout << "out_format_tree:dataType: " << p_game_obj->out_format_tree.get<int>("dataType") << endl;
}

void CharacterCtrl::GetDetailInfo(GlobalGameObj *p_game_obj)
{
    int character_id;
	try
	{
	    // because player can query other player's info, character id is needed
		character_id = p_game_obj->in_tree.get<int>("characterId");

	}
	catch(exception &ex)
	{
		p_game_obj->error_code = Common::JsonTreeError;
		p_game_obj->error_str = "in GetDetailInfo";
		return;
	}

	ptree info_tree;
	Character::GetDetailInfoTree(p_game_obj, character_id, info_tree);

	info_tree.put("evt", "Character_DetailInfo");
	info_tree.put("code", 0);

    p_game_obj->out_tree.push_back(make_pair("", info_tree));

    p_game_obj->out_format_tree.put("dataType", Common::NormalData);
}

